#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdio>
#include <time.h>
#include <cmath>
#include "Zombie.hpp"
#include "TileMap.hpp"

void killWindow(sf::RenderWindow& window) { window.close(); }

int main()
{
    //do clocky stuff
    int seed = time(NULL);
    srand(seed);
    sf::Clock clock;
    //printf("Seed: %d\n", seed);

    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 800), "Creeper Madness");
    sf::View view(sf::FloatRect(0.0f, 0.0f, 80.0f, 80.0f));
    window.setView(view);
    window.setFramerateLimit(30);
    sf::Vector2f target_size = view.getSize();

    bool _ShowHelp;
    sf::Font font;
    font.loadFromFile("arial.ttf");
    sf::Text _HelpText("-Help-\nPress H to unhide");
    _HelpText.setFont(font);
    _HelpText.setCharacterSize(16);
    _HelpText.setColor(sf::Color::Cyan);

    TileMap myMap(100, 100, 8, 8);

    Zombie myCreeper("Johnny");

    myCreeper.setPosition(window.getSize().x/2, window.getSize().y/2);

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event Event;
        while (window.pollEvent(Event))
        {
            if (Event.type == sf::Event::Closed)
            {
                killWindow(window);
            }
            if (Event.type == sf::Event::KeyPressed)
            {
                switch (Event.key.code)
                {
                    case sf::Keyboard::Escape:
                        killWindow(window);
                    break;
                    case sf::Keyboard::H:
                        _ShowHelp = !_ShowHelp;
                        if (!_ShowHelp)
                        {
                            _HelpText.setString("-Help-\nPress H to unhide");
                        }
                        else
                        {
                            _HelpText.setString("-Help-\nH - Hide help\nWASD - Navigate\nMousewheel - Zoom in/out\nN - Generate new tiles");
                        }
                    break;
                    case sf::Keyboard::N:
                        myMap.newMap();
                    break;
                    case sf::Keyboard::Space:
                       myCreeper.jump();
                    break;
                    default:
                    break;
                }
            }
            if (Event.type == Event.MouseWheelMoved)
            {
                if (Event.mouseWheel.delta < 0)
                {
                    target_size *= 1.1f;
                }
                if (Event.mouseWheel.delta > 0)
                {
                    target_size *= 0.9f;
                }
            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            myCreeper.setScale(1,1);
            myCreeper.move(-1, 0);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            myCreeper.setScale(-1,1);
            myCreeper.move(1, 0);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            myCreeper.move(0, -1);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            myCreeper.move(0, 1);
        }

        myCreeper.update(1.0f / 30.0f);
        // Clear screen
        window.clear();

        // Move the view's center 10% of the way towards the creeper
        view.setCenter(view.getCenter() * 0.9f + myCreeper.getPosition() * 0.1f);
        view.setSize(view.getSize() * 0.9f + target_size * 0.1f);
        window.setView(view);

        window.draw(myMap);
        window.draw(myCreeper);

        //Draw GUI stuff into seperate, default view
        window.setView(window.getDefaultView());
        window.draw(_HelpText);

        // Update the window
        window.display();
    }

    return EXIT_SUCCESS;
}
